package gensql;


import java.io.*;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

/**
 * @author liyanhao
 * @Date 2024-01-02 17:01
 */
public class GenHlaSql {
    public static void main(String[] args) {
//        updatePrice();
        //新编码
        InsertSkuSql();
        //in语句拼接
//        inSql();

//        System.out.println(DateUtil.beginOfDay(new Date()));
        //忽略订单
//        deleteOrder();

//        String ss = null;
//        try {
//            ss = "aa";
//            int i = 0;
//            int i2 = 0;
//            ss = String.valueOf(i2 / i);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//            System.out.println(ss);
//        }
    }

    private static void deleteOrder() {
        ArrayList<String> list = new ArrayList<>();
        String defaultPath = "tools\\src\\main\\java\\gensql\\sku";
        String statePath = Paths.get(defaultPath).toAbsolutePath().toString();

        try (BufferedReader br = new BufferedReader(new FileReader(statePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                //处理每一行的代码放在这里
                String[] split = line.split("\\t");
                String var1 = split[0];
                String s = deleteOrder(var1);
                list.add(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        File file = new File(Paths.get("tools\\src\\main\\java\\gensql\\outsql.sql").toAbsolutePath().toString());


        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for (String str : list) {
                writer.write(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void inSql() {
        ArrayList<String> list = new ArrayList<>();
        String defaultPath = "tools\\src\\main\\java\\gensql\\in语句拼接";
        String statePath = Paths.get(defaultPath).toAbsolutePath().toString();

        try (BufferedReader br = new BufferedReader(new FileReader(statePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                //处理每一行的代码放在这里
                String s = "'"+line+"',";
                list.add(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        File file = new File(Paths.get("tools\\src\\main\\java\\gensql\\outsql.sql").toAbsolutePath().toString());


        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for (String str : list) {
                writer.write(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void InsertSkuSql() {
        ArrayList<String> list = new ArrayList<>();
        String defaultPath = "tools\\src\\main\\java\\gensql\\sku";
        String statePath = Paths.get(defaultPath).toAbsolutePath().toString();

        try (BufferedReader br = new BufferedReader(new FileReader(statePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                //处理每一行的代码放在这里
                String[] split = line.split("\\t");
                String var1 = split[0];
                String var2 = split[1];
                String var3 = split[2];
                String s = InsertSkuSql(var1, var2, var3);
                list.add(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        File file = new File(Paths.get("tools\\src\\main\\java\\gensql\\outsql.sql").toAbsolutePath().toString());


        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for (String str : list) {
                writer.write(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private static void updatePrice() {
        ArrayList<String> list = new ArrayList<>();
        String defaultPath = "tools\\src\\main\\java\\gensql\\sku";
        String statePath = Paths.get(defaultPath).toAbsolutePath().toString();

        try (BufferedReader br = new BufferedReader(new FileReader(statePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                //处理每一行的代码放在这里
                String[] split = line.split("\\t");
                if (split.length < 3) {
                    System.out.println(Arrays.toString(split));
                    break;
                }
                String var1 = split[0];
                String var2 = split[1];
                String var3 = split[2];
//                String s = updatePrice(var1, var2, var3);
                String s = updatePrice(var2, var1, var3);
                list.add(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        File file = new File(Paths.get("tools\\src\\main\\java\\gensql\\outsql.sql").toAbsolutePath().toString());


        try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
            for (String str : list) {
                writer.write(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String InsertSkuSql(String var1, String var2, String var3) throws IOException {
        String sql1 = "\n" +
                "insert into o_autoerp_erpsku (erp_goods_no, cost) VALUES\n" +
                "('" + var2 + "', '" + var3 + "')\n" +
                "as neo\n" +
                "on duplicate key update cost = neo.cost;";
        String sql2 = "insert into o_autoerp_skumap (ext_sku_no, erp_goods_no) VALUES\n" +
                "('" + var1 + "', '" + var2 + "');";
        return sql1 + sql2;

    }
    private static String deleteOrder(String var1) throws IOException {
        String sql = "UPDATE o_autoerp_log_hla SET flush_status = 1 where shang_pin_yuan_shi_jiao_yi_bian_hao =  '"+var1+"'; \n";
        return sql;

    }

    private static String updatePrice(String var1, String var2, String var3) throws IOException {
        String sql13 = "update o_autoerp_erpsku SET cost ='"+var3+"' where erp_goods_no ='"+var1+"';\n";
        return sql13;

    }
}
